home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2940 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.0 KB

  1. Path: ecf2.puc.edu!bivey
  2. From: bivey@ecf2.puc.edu (Bruce Ivey)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Keyboard input: C's equivalent to BASIC's Inkey$
  5. Date: 25 Jan 1996 03:33:27 GMT
  6. Organization: CRL Dialup Internet Access
  7. Message-ID: <4e6tm7$ntc@nntp.crl.com>
  8. References: <Pine.SUN.3.91.960111212844.17033A@suntan>
  9. NNTP-Posting-Host: ecf2.puc.edu
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Carlos Diaz (CS) (cdiaz@eng.usf.edu) wrote:
  13. : Hi! Can someone tell me what the C equivalent of BASIC's Inkey$ command 
  14. : is? In particular, I'm looking to translate the following BASIC code to 
  15. : C. It's the old, "wait for a key- if there is  no key do something else 
  16. : then wait for a key, etc".
  17.  
  18. In Borland C/C++, try
  19.  
  20. int readKey()
  21. {
  22.    int key = 0;
  23.  
  24.    if (kbhit())
  25.    {
  26.       key = getch();      /* a key was pressed; read it */
  27.       if (key == 0)       /* if zero, this is a two-char Fn key */
  28.          key = -getch();  /* read 2nd char & return as negative */
  29.    }                      /* to signal that this is an F-key */
  30.    return key;
  31. }
  32.